<?php
declare (strict_types = 1);

namespace app\course\service\video\admin;

use app\common\exception\LogicException;
use app\common\service\BaseService;
use app\course\logic\video\admin\ExamineVideoSection as ExamineVideoSectionLogic;
use app\course\logic\video\admin\Video as VideoLogic;
use app\school\model\school\School as SchoolModel;
use app\user\logic\teacher\admin\Teacher as TeacherLogic;
use eduline\admin\libs\pageform\FormItem;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageList;
use think\facade\Request;

/**
 * 点播课程的课时审核-后台
 */
class ExamineVideoSection extends BaseService
{
    /**
     * 列表
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-23
     * @return   [type]                         [description]
     */
    public function index()
    {
        // 搜索功能
        $map                                                = [];
        Request::has('course_title', 'get', true) && $map[] = ['video.title', 'like', '%' . Request::get('course_title/s') . '%'];
        Request::has('mhm_id', 'get', true) && $map[]       = ['video.mhm_id', '=', Request::get('mhm_id/d')];

        $is_platform = request()->is_platform;

        $data = app(ExamineVideoSectionLogic::class)->getList($map);
        // 定义字段
        $keyList = [
            'school_name'  => ListItem::make()->title('机构')->visible($is_platform),
            'course_title' => ListItem::make()->title('课程名称'),
            'title'        => ListItem::make()->title('课时名称'),
            'teacher_name' => ListItem::make()->title('讲师'),
        ];

        $searchKey = [
            'mhm_id'       => FormItem::make('searchSchool')->title(false)->visible($is_platform),
            'course_title' => FormItem::make()->title(false)->placeholder('课程名称'),
        ];
        // 设置表单
        $list = app(PageList::class);
        // 表单字段
        $list->pageKey   = $keyList;
        $list->searchKey = $searchKey;
        $list->datas     = $data;

        return $list->send();
    }

    /**
     * 审核课时
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-10
     * @param    [type]                         $id [description]
     * @return   [type]                             [description]
     */
    public function handle(int $id)
    {
        if (Request::isPost()) {
            $data = Request::post();
            app(ExamineVideoSectionLogic::class)->doExamine($id, $data);

            return $this->parseToData([], 1, '审核完成');
        }

        $data = app(ExamineVideoSectionLogic::class)->with(['course_data','video.coverUrl'])->where('id', $id)->whereIn('status', '2,3')->findOrFail()->bindAttr('course_data', ['data_type_text', 'data_type'])->bindAttr('video',['cover_url']);

        $mhm_id = request()->user->getAttr('mhm_id');
        if ($mhm_id != 1) {
            // 检测机构权限,机构只能审核本机构的
            $course = app(VideoLogic::class)->where('mhm_id', $mhm_id)->where('id', $data->getAttr('course_id'))->findOrEmpty();
            if ($course->isEmpty()) {
                throw new LogicException('你没有审核权限');
            }
        } else {
            $course = app(VideoLogic::class)->where('id', $data->getAttr('course_id'))->findOrEmpty();
        }

        $data->school_name  = app(SchoolModel::class)->getSchoolNameById($course->mhm_id);
        $data->teacher_name = app(TeacherLogic::class)->getTeacherNameById($course->teacher_id);
        $data->course_title = $course->title;
        $data->hidden(['video']);

        return $this->parseToData($data, 1);
    }
}
